package com.ycd.sync;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import java.nio.charset.StandardCharsets;

/**
 * 同步消息是发送者发送消息，需要等待结果的返回，才能继续发送第二条消息
 */
public class producer {
    public static Message message;

    public static void main(String[] args) throws Exception {
        // 1.创建生产者对象
        DefaultMQProducer producer = new DefaultMQProducer("Hi_hello_word_sync");
        // 2.设置nameserver连接
        producer.setNamesrvAddr("localhost:9876");
        // 3.启动生产者
        producer.start();
        // 4.设置消息
        for (int i = 0; i < 10; i++) {
            message = new Message("sync", "bbb", ( i + "世界你好！！").getBytes(StandardCharsets.UTF_8));
            // 5.将消息同步发送到broker
            SendResult result = producer.send(message);
            System.out.println(result);
        };
        // 6.关闭生产者
        producer.shutdown();
    }
}
